package org.itstack.demo.netty.multicast.client;

import java.net.*;
import java.util.Arrays;
import java.util.List;

/**
 * @author yichuan@iscas.ac.cn
 * @version 1.0
 * @date 2021/10/20 14:45
 */
public class MulticasReceiveNetworkCard {
    public static void main(String[] args) throws Exception
    {
        //InetAddress inetRemoteAddr = InetAddress.getByName("224.0.0.5");
        InetAddress group = InetAddress.getByName("224.2.1.1");
        DatagramPacket recvPack = new DatagramPacket(new byte[1024], 1024);


        //MulticastSocket server = new MulticastSocket(8888);
        int port = 9000;
        MulticastSocket socket = new MulticastSocket(port);
        //将有效网卡加入组播
        socket.joinGroup(group);
        /*
         * 如果是发送数据报包,可以不加入多播组; 如果是接收数据报包,必须加入多播组; 这里是接收数据报包,所以必须加入多播组;
         */
        //server.joinGroup(inetRemoteAddr);

        System.out.println("---------------------------------");
        System.out.println("SocketServer current start......");
        System.out.println("---------------------------------");

        while (true)
        {
            socket.receive(recvPack);
            //server.receive(recvPack);

            byte[] recvByte = Arrays.copyOfRange(recvPack.getData(), 0,
                    recvPack.getLength());

            System.out.println("SocketServer receive msg:" + new String(recvByte));
        }
    }
}

